package org.ziegler.log.analysis.resultHandler;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;

import org.ziegler.log.analysis.LogCore;
import org.ziegler.log.analysis.core.SplitColumn;

public class ErrKeyResultHandler implements ResultHandler {

    private final String errKey;

    private final List<SplitColumn> result = new ArrayList<>();

    public ErrKeyResultHandler(String errKey) {
        this.errKey = Objects.requireNonNull(errKey);
    }

    @Override
    public void addResult(SplitColumn splitColumn) {
        if (StringUtils.isNotBlank(splitColumn.getKey())) {
            if (errKey.equals(splitColumn.getKey())) {
                result.add(splitColumn);
            }
        }

    }

    @Override
    public void addComplete() {
        result.sort(Comparator.comparing(SplitColumn::getDateTime).reversed());
    }

    @Override
    public void output() {
        LogCore.info("errKey = " + errKey + ",总数目 = " + result.size());
        result.stream().limit(100).forEach(column -> LogCore.log(column.getStr()));
    }

}
